package omm.domain.evolutiongraph;

import java.util.Map;
import java.util.TreeMap;

import org.joda.time.DateTime;
import org.joda.time.DateTimeComparator;

public class AnualByMonthPeriodStrategy extends AnualPeriodStrategy {

	final private Integer periods;
	final private DateTime end;

	public AnualByMonthPeriodStrategy(int canvasWidth, DateTime end) {
		super(canvasWidth);
		this.periods = 12;
		this.end = end;//Fin del mes pasado
	}

	@Override
	public Map<DateTime, Double> calculatePeriods() {
		Map<DateTime, Double> periodsBars = new TreeMap<DateTime, Double>(
				DateTimeComparator.getDateOnlyInstance());
		
		//Carga de fechas
		//DateTime actualTime = new DateTime(System.currentTimeMillis());
		//DateTime time = new DateTime(actualTime.getYear(), actualTime.getMonthOfYear(), 1, 0, 0, 0);
		// Harcodeo si hay una base vieja
//		DateTime time = new DateTime(2011,8, 1, 0, 0, 0);
		

		int iterations = periods;
		DateTime itTime = end;
		int year = itTime.getYear();
		int month = itTime.getMonthOfYear();
		for (int i = 0; i < iterations; i++) {
			itTime = new DateTime(year, month, 1, 0, 0, 0);
			periodsBars.put(itTime, 0.0);
			if (month == 1) {
				year = itTime.getYear() - 1;
				month = 12;
			} else {
				month -= 1;
			}

		}
		return periodsBars;
	}

	@Override
	protected Integer getPeriodsQuantity() {
		return periods;
	}

	@Override
	public String getTitle() {
		return "mensual";
	}

	@Override
	public DateTime getBegin() {
		return end.minusMonths(periods-1);
	}

	@Override
	public DateTime getEnd() {
		return end;
	}

	
	
	
}